﻿//图层控制单元组件
//
function LayerOperateItemBar(layerInfo,operType) {
    //属性
    this.layerInfo = layerInfo;
    this.operType = operType;
    this.operList = ["load", "location", "alpha","symbol"];
    if (typeof this.CreateLayerOperateItemBar != "function") {
        LayerOperateItemBar.prototype.CreateLayerOperateItemBar = function () {
            return CreateLayerOperateItemBar(this.layerInfo, this.operList, this.operType);
        };
    }
    //创建图层控制单元
    function CreateLayerOperateItemBar(layerInfo, operList, operType) {
        var lyInfo = {};
        lyInfo.id = layerInfo.GUID;
        lyInfo.name = layerInfo.ALIAS;
        lyInfo.positon = layerInfo.POSITION;
        lyInfo.option = layerInfo.OPTION;
        //创建图层控制单元
        var layeroperate_itembar = document.createElement("div");
        layeroperate_itembar.className = "layeroperate-itembar-main";

        //创建图层名及图标栏
        var layeroperate_itember_layerbar = document.createElement("div");
        layeroperate_itember_layerbar.className = "layeroperate-itembar-layerbar";
        layeroperate_itembar.appendChild(layeroperate_itember_layerbar);

        //创建图层名span
        var layeroperate_itembar_layerbar_layername = document.createElement("span");
        var charCount=lyInfo.name.replace(/[\u0391-\uFFE5]/g, "aa").length;
        if (charCount > 36) {
            layeroperate_itembar_layerbar_layername.title = lyInfo.name;
            var lyInfoChar = "";
            var charSum=0;
            for (var i = 0; i < lyInfo.name.length; i++) {
                var charCode = lyInfo.name.charCodeAt(i);
                if (charCode > 255) 
                    charSum = charSum + 2;
                else charSum++;
                if (charSum >= 34) {
                    lyInfo.name = lyInfo.name.substr(0,i)+"...";
                    break;
                }
            }
                layeroperate_itembar_layerbar_layername.innerText = lyInfo.name;
        } 
        else
            layeroperate_itembar_layerbar_layername.innerText = lyInfo.name;
        layeroperate_itember_layerbar.appendChild(layeroperate_itembar_layerbar_layername);

        if (!layerInfo.SERVICETYPE === sw_servicetype.sw_terrain) {
            //创建图层定位
            var layeroperate_itembar_layerbar_location = document.createElement("div");
            layeroperate_itembar_layerbar_location.className = "layeroperate-itembar-layerbar-location";
            layeroperate_itembar_layerbar_location.title = "定位";
            layeroperate_itembar_layerbar_location.onclick = function () {
                //调用定位方法
                //LayerLocation(lyInfo.id, m_LoadLayerInfo);
            }
            layeroperate_itember_layerbar.appendChild(layeroperate_itembar_layerbar_location);

            if (!layerInfo.SERVICETYPE === sw_servicetype.sw_oblique) {
                //设置图层显示顺序置后
                var layeroperate_itembar_layerbar_down = document.createElement("div");
                layeroperate_itembar_layerbar_down.className = "layeroperate-itembar-layerbar-down";
                layeroperate_itembar_layerbar_down.title = "图层置后";
                layeroperate_itembar_layerbar_down.onclick = function () {
                    //设置图层项显示顺序、图层加载顺序
                    //var lyItemDownBar = $(this);
                    //LayerMove(lyItemDownBar, lyInfo.id, "down", m_LoadLayerInfo);

                }
                layeroperate_itember_layerbar.appendChild(layeroperate_itembar_layerbar_down);

                //设置图层显示顺序置前
                var layeroperate_itembar_layerbar_up = document.createElement("div");
                layeroperate_itembar_layerbar_up.className = "layeroperate-itembar-layerbar-up";
                layeroperate_itembar_layerbar_up.title = "图层置前";
                layeroperate_itembar_layerbar_up.onclick = function () {
                    //设置图层项显示顺序、图层加载顺序
                    //var lyItemUpBar = $(this);
                    //LayerMove(lyItemUpBar, lyInfo.id, "up", m_LoadLayerInfo);

                }
                layeroperate_itember_layerbar.appendChild(layeroperate_itembar_layerbar_up);
            }
            

            //创建图层透明度控制滑块
            var layeroperate_itembar_alphabar = document.createElement("div");
            layeroperate_itembar_alphabar.className = "layeroperate-itembar-alphabar";
            layeroperate_itembar.appendChild(layeroperate_itembar_alphabar);

            var layeroperate_itembar_alphabar_text = document.createElement("span");
            layeroperate_itembar_alphabar_text.innerText = "透明度：";
            layeroperate_itembar_alphabar.appendChild(layeroperate_itembar_alphabar_text);

            var layeroperate_itembar_alpha = document.createElement("input");
            layeroperate_itembar_alpha.className = "single-slider";
            layeroperate_itembar_alpha.id = "layeroperate_itembar_alpha" + lyInfo.id;
            var thisjRangeValue = null;
            $(function () {
                thisjRangeValue = $("#" + layeroperate_itembar_alpha.id).jRange({
                    onstatechange: function (evt) {
                        if (m_LoadLayerInfo[lyInfo.id].VISIBLE)
                            SetLayerOpacity(lyInfo.id, m_LoadLayerInfo);
                    },
                    from: 0,
                    to: 100,
                    step: 1,
                    scale: [0, 50, 100],
                    format: '%s',
                    width: 200,
                    showLabels: true,
                    showScale: false

                });
            });
            //        onChange: function (e, i) {
            //            /*e, range元素的当前值；i， 是该值占max的百分之多少。*/
            //            SetLayerOpacity(lyInfo.id, m_LoadLayerInfo);
            //        }
            layeroperate_itembar_alphabar.appendChild(layeroperate_itembar_alpha);
        }
        return layeroperate_itembar;
    }

    //数据操作
    //图层定位
    function LayerLocation(id,loadLayerInfo) {
        var locationLayer = loadLayerInfo[id]; //获取要定位的图层信息
        var postions = locationLayer.POSITION;
        if (postions!="")
            flyToLocation(postions.split(','));
    }

    //图层加载排序
    function LayerMove(item, id, operType, loadLayerInfo) {
        //设置控件顺序


        //获取已加载的地图图层对象
        var thisLayerInfo = loadLayerInfo[id];
        var layerHelp = new LayerHelper(thisLayerInfo);
        var thisLayer = layerHelp.GetLayer();
        var layerIndex = thisLayer.getZIndex();
        var layerCount = map.getLayers().getLength();
        if (operType == "up") {//置前
            if (layerIndex < layerCount - 1) {
                map.getLayers().getArray()[layerIndex + 1].setZIndex(layerIndex);
                layerIndex++;
            }
        }
        else if (operType == "down") { //置后
            if (layerIndex > 0) {
                map.getLayers().getArray()[layerIndex - 1].setZIndex(layerIndex);
                layerIndex--;

            }

        }
        thisLayer.setZIndex(layerIndex);
    }

    //透明度控制
    function SetLayerOpacity(id, loadLayerInfo) {
        var opacity = $("#layeroperate_itembar_alpha"+id).val();
        if (opacity == undefined) opacity = 1;
        else
            opacity = (100-opacity) / 100;
        var thisLayerInfo = loadLayerInfo[id];
        var layerHelp = new LayerHelper(thisLayerInfo);
        var thisLayer = layerHelp.GetLayer();
        thisLayer.setOpacity(opacity);
    }
}
